﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Resources;
using System.Collections;
using System.Collections.Specialized;

namespace LANmine
{
    public class Language
    {
        private OrderedDictionary _langDict = new OrderedDictionary();

        public OrderedDictionary LangDict
        { get { return _langDict; } }

        private ResXResourceReader _resRdr;

        /**
         * Constructor
         * Spawns a ResourceReader with the right language
         */
        public Language(String lang)
        {
            /** Describe all languages */
            _langDict.Add("en", "English");
            _langDict.Add("nl", "Nederlands");
            _langDict.Add("fr", "Français");

            /** Create resource reader */
            _resRdr = new ResXResourceReader("Resources/Language." + lang + ".resx");

            /** Make sure our reader works */
            try { this.GetString("ResxTest"); }
            catch (Exception) { throw; }
        }

        /**
         * Language::GetString()
         * Get a specific dictionary value from the .resx file
         */
        public String GetString(String key)
        {
            try
            {
                /** Look for the given string in the dictionary */
                foreach (DictionaryEntry d in _resRdr)
                {
                    if (d.Key.ToString() == key)
                        /** String found in dictionary, return the string */
                        return d.Value.ToString();
                }

                /** Error looking up string */
                return "ValNotFound";
            }
            catch (Exception) { throw; }           
        }

        /**
         * Destructor
         * Frees up the handle on the .resx file
         */
        ~Language()
        {
            _resRdr.Close();
        }
    }
}
